function This = mysstateswap(This,Opt)
% mysstateswap  [Not a public function] Swap endogeneity and exogeneity of some parameters and some transition variables for steady-state solver.
%
% Backend IRIS function.
% No help provided.

% -IRIS Toolbox.
% -Copyright (c) 2007-2015 IRIS Solutions Team.

%--------------------------------------------------------------------------

lsEndg = Opt.endogenise;
lsExg = Opt.exogenise;

if isempty(lsEndg) && isempty(lsExg)
    return
end
if ~isempty(lsEndg) && ischar(lsEndg)
    lsEndg = regexp(lsEndg,'\w+','match');
end
if ~isempty(lsExg) && ischar(lsExg)
    lsExg = regexp(lsExg,'\w+','match');
end

endgPos = mylookupname(This,lsEndg);
exgPos = mylookupname(This,lsExg);
ixEndgValid = ~isnan(endgPos);
ixExgValid = ~isnan(exgPos);

chkValid( );

% Swap the variables and parameters.
This.nametype(endgPos) = 2;
This.nametype(exgPos) = 4;

inx = any(isnan(This.Assign(1,exgPos,:)),3);
if any(inx)
    utils.error('model:mysstateswap', ...
        'This variable is exogenised to NaN: ''%s''.', ...
        This.name{exgPos(inx)});
end

return

    
    
    
    function chkValid()
        if all(ixEndgValid)
            ixEndgValid = This.nametype(endgPos) == 4;
        end
        if all(ixExgValid)
            ixExgValid = This.nametype(exgPos) == 2;
        end
        if any(~ixEndgValid)
            utils.error('model:mysstateswap', ...
                'Cannot endogenise this name: ''%s''.', ...
                lsEndg{~ixEndgValid});
        end
        if any(~ixExgValid)
            utils.error('model:mysstateswap', ...
                'Cannot exogenise this name: ''%s''.', ...
                lsExg{~ixExgValid});
        end
        nEndo = length(lsEndg);
        nExo = length(lsExg);
        if nEndo ~= nExo
            utils.error('model:mysstateswap', ...
                ['The number of exogenised variables and the number ', ...
                'endogenised parameters must match.']);
        end
    end
end
